package UIComponents
{
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;

	public class ShopItemFrame extends Sprite
	{
		private var _buyBtnSp:DisplayObjectContainer;
		private var _buyBtn:MyButton;
		private var _itemPrice:TextField;
		private var _piliang:TextField;
		private var _piliangsp:DisplayObjectContainer;
		private var _shopItem:IGD_ShopItem;
		public var _shopMb:DisplayObject;
		public var _zhekouIcon:DisplayObject;

		public function ShopItemFrame()
		{
			init();
		}

		public function init():void
		{
			this._shopMb=this.getChildByName("shopMb");
			this._buyBtnSp=this.getChildByName("buyBtnContainer") as DisplayObjectContainer;
			this._buyBtn=this._buyBtnSp.getChildByName("buyBtn") as MyButton;
			this._itemPrice=this._buyBtnSp.getChildByName("itemPrice") as TextField;
			this._itemPrice.selectable=false;
			this._itemPrice.mouseEnabled=false;
			this._piliangsp=this.getChildByName("piliang") as DisplayObjectContainer;
			this._piliang=_piliangsp.getChildByName("txt_num") as TextField;
			this._piliang.selectable=false;
			this._piliang.mouseEnabled=false;
			_zhekouIcon=this.getChildByName("zhekouIcon");
			addListerFun();
		}
		
		public function setData(shopItem:IGD_ShopItem):void
		{
			this._shopMb.visible=false;
			this._shopItem=shopItem;
			var tips:String=shopItem.itemName+"："+shopItem.itemDes;
			(this.getChildByName("itemIconContainer") as IconContainer).addPic(shopItem.shopItemPic,tips);
			this.controlFlag(shopItem.shopItemState);
			if(shopItem.isReachLv)
			{
				this.getChildByName("needLevel").visible=false;
				this._piliangsp.visible=
				this._buyBtnSp.visible=true;
				this._piliang.text=String(shopItem.perNum);
				this._itemPrice.text=String(shopItem.price);
				this._itemPrice.selectable=false;
				this._itemPrice.mouseEnabled=false;
				if(shopItem.moneyType==0)
				{
					this._buyBtnSp.getChildByName("obIcon").visible=!FlatInfo.isOPenQQVip;
					this._buyBtnSp.getChildByName("qb_icoin").visible=FlatInfo.isOPenQQVip;
					this._buyBtnSp.getChildByName("jb_icoin").visible=false;
				}else{
					this._buyBtnSp.getChildByName("qb_icoin").visible=this._buyBtnSp.getChildByName("obIcon").visible=false;
					this._buyBtnSp.getChildByName("jb_icoin").visible=true;
				}
			}else{
				this._piliangsp.visible=
				this._buyBtnSp.visible=false;
				this.getChildByName("needLevel").visible=true;
				((this.getChildByName("needLevel") as DisplayObjectContainer).getChildByName("needLv") as TextField).text="需要等级"+shopItem.needLever;
			}
			
			
		}


		public function addListerFun():void
		{
			this._buyBtn.addEventListener(ComponentEvent.BUTTON_DOWN, buyfun);
//			this.addEventListener(Event.EXIT_FRAME, relse);
		}

		/**
		 * 购买功能 
		 * @param e
		 */
		public function buyfun(e:ComponentEvent):void
		{
			_shopItem.sendBuyCommand();
		}

		/**
		 * 释放资源 
		 * @param evt
		 */		
		public function relse(evt:Event):void
		{
			this._buyBtn.removeEventListener(ComponentEvent.BUTTON_DOWN, buyfun);
//			this.removeEventListener(Event.EXIT_FRAME, relse);
		}

		/**
		 *  控制标签的显示
		 *   0下架  1上架  2热卖  3新品
		 * @return
		 */
		public function controlFlag(flag:Number):void
		{
			switch (flag)
			{
				case 2:
					this.getChildByName("hotFlag").visible=true;
					this.getChildByName("newFlage").visible=false;
					break;
				case 3:
					this.getChildByName("hotFlag").visible=false;
					this.getChildByName("newFlage").visible=true;
					break;
				default:
					this.getChildByName("hotFlag").visible=false;
					this.getChildByName("newFlage").visible=false;
					break;
			}
		}
	}
}